﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      IAM Common Helper
// Created By:     TingRong Shi (trshi@wicresoft.com)
// Created Date:   2011-4-25
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ASB.CC.Business
{
    public static class IAMHelper
    {
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        public static List<Role> GetRoleList()
        {
            using (var dc = new CCEntities())
            {
                List<Role> roles = dc.Roles.Where(p => p.Disabled == false && (p.BelongsTo == "CCD" || p.BelongsTo == "CCO" || p.Name == "CIO Admin" || p.Name == "CIO User" || p.Name == "CID Admin" || p.Name == "CID Reviewer")).ToList<Role>();
                return roles;
            }
        }

        /// <summary>
        /// 获取国家权限列表
        /// </summary>
        /// <returns></returns>
        public static List<Role> GetCountryList()
        {
            using (var dc = new CCEntities())
            {
                List<Role> roles = new List<Role>();
                foreach (var country in dc.Countries.Where(p => p.Disabled == false).ToArray())
                {
                    string countryid = country.CountryID.ToString();
                    var role1 = dc.Roles.FirstOrDefault(p => p.Name == countryid && p.Disabled == false && p.Description == "+country+");
                    if (role1 != null)
                    {
                        Role role = new Role();
                        role.Name = country.Name;
                        country.Areas.Load();
                        if (country.Areas != null && country.Areas.Count != 0)
                            role.Description = country.Areas.First().Name;
                        role.RoleID = role1.RoleID;
                        roles.Add(role);
                    }
                }
                return roles;
            }
        }

        /// <summary>
        /// 通过User的Cil获取权限
        /// </summary>
        /// <param name="cil"></param>
        /// <returns></returns>
        public static List<Role> GetRoleByUserID(string cil)
        {
            using (var dc = new CCEntities())
            {
                Employee em = dc.Employees.FirstOrDefault(p => p.EmployeeNo == cil);
                if (em == null)
                    return null;
                em.Roles.Load();
                List<Role> roles = em.Roles.Where(p => p.Disabled == false && (p.BelongsTo == "CCD" || p.BelongsTo == "CCO" || p.Name == "CIO Admin" || p.Name == "CIO User" || p.Name == "CID Admin" || p.Name == "CID Reviewer")).ToList<Role>();
                return roles;
            }
        }

        /// <summary>
        /// 由User的CIL获取所对应的Country权限
        /// </summary>
        /// <param name="cil"></param>
        /// <returns></returns>
        public static List<Role> GetCountryByUserID(string cil)
        {
            using (var dc = new CCEntities())
            {
                Employee em = dc.Employees.FirstOrDefault(p => p.EmployeeNo == cil);
                if (em == null)
                    return null;
                em.Roles.Load();
                List<Role> roles = new List<Role>();
                foreach (var country in dc.Countries.Where(p => p.Disabled == false).ToArray())
                {
                    string countryid = country.CountryID.ToString();
                    var role1 = em.Roles.FirstOrDefault(p => p.Name == countryid && p.Disabled == false && p.Description == "+country+");
                    if (role1 != null)
                    {
                        Role role = new Role();
                        role.Name = country.Name;
                        role.RoleID = role1.RoleID;
                        roles.Add(role);
                    }
                }
                return roles;
            }
        }

        /// <summary>
        /// 获取CostcernerHead角色ID
        /// </summary>
        /// <returns></returns>
        public static int GetIsCostcernerHeadID()
        {
            using (var dc = new CCEntities())
            {
                var role = dc.Roles.FirstOrDefault(p => p.Disabled == false && p.Name == "CostCenterHead");
                return role == null ? 0 : role.RoleID;
            }
        }

        public static List<string> GetAllUser()
        {
            using (var dc = new CCEntities())
            {
                List<string> employees = new List<string>();
                var roles = dc.Roles.Where(p => p.Disabled == false && (p.BelongsTo == "CIO" || p.BelongsTo == "CCD" || p.BelongsTo == "CCO" || p.BelongsTo == "CCD") && p.Description != "+country+" && p.Description != "+pcat+").ToArray();
                foreach (var role in roles)
                {
                    role.Employees.Load();
                    foreach (var employee in role.Employees)
                        employees.Add(employee.EmployeeNo);
                }
                return employees.Distinct().ToList<string>();
            }
        }

        /// <summary>
        /// 获取所有Cluster
        /// </summary>
        /// <returns></returns>
        public static List<Cluster> GetAllClusters()
        {
            using (var dc = new CCEntities())
            {
                return dc.Clusters.Where(p => p.Disabled == false).ToList<Cluster>();
            }
        }

        /// <summary>
        /// 获取所有Province
        /// </summary>
        /// <returns></returns>
        public static List<Province> GetAllProvinces()
        {
            using (var dc = new CCEntities())
            {
                var provinces = dc.Provinces.Where(p => p.Disabled == false).ToList<Province>();
                foreach(var province in provinces)
                    province.Clusters.Load();
                return provinces;
            }
        }

        /// <summary>
        /// 获取所有Category
        /// </summary>
        /// <returns></returns>
        public static List<Category> GetAllCategories()
        {
            using (var dc = new CCEntities())
            {
                return dc.Categories.Where(p => p.Disabled == false).ToList<Category>();
            }
        }

        /// <summary>
        /// 获取用户所对应的CostCenter
        /// </summary>
        /// <param name="cil"></param>
        /// <returns></returns>
        public static DataTable GetCostCenterByUserCIL(string cil)
        {
            DataTable dt = new DataTable();
            //string server = ConfigurationManager.AppSettings["Server"];
            //string uid = ConfigurationManager.AppSettings["User"];
            //string pwd = ConfigurationManager.AppSettings["Password"];
            //string database = ConfigurationManager.AppSettings["DataBase"];
            string connString = "server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=" + database + "";
            SqlConnection conn = new SqlConnection(connString);
            string sqlString = " SELECT a.EmployeeNo,c.RoleName, a.UserRoleID, a.RoleID, d.DicItemKey CostCenterCode ,d.dicitemid CostCenterId "
                        + "FROM UserRole a  INNER JOIN  Role c ON a.RoleID = c.RoleID "
                        + "INNER JOIN CostCenterHead b ON a.UserRoleID=b.UserRoleID "
                        + "INNER JOIN Dicitem d ON b.CostCenterID=d.dicitemid "
                        + "WHERE  c.RoleName = 'CostCenterHead' AND a.EmployeeNo='" + cil + "' ";
            SqlDataAdapter adapter = new SqlDataAdapter(sqlString, conn);
            adapter.Fill(dt);
            return dt;
        }

        public static string server
        {
            get
            {
                if (String.IsNullOrEmpty(_server))
                    _server = ConfigurationManager.AppSettings["Server"];
                return _server;
            }
        }
        public static string uid
        {
            get
            {
                if (String.IsNullOrEmpty(_uid))
                    _uid = ConfigurationManager.AppSettings["User"];
                return _uid;
            }
        }
        public static string pwd
        {
            get
            {
                if (String.IsNullOrEmpty(_pwd))
                    _pwd = ConfigurationManager.AppSettings["Password"];
                return _pwd;
            }
        }
        public static string database
        {
            get
            {
                if (String.IsNullOrEmpty(_database))
                    _database = ConfigurationManager.AppSettings["DataBase"];
                return _database;
            }
        }
        private static string _server = "";
        private static string _uid = "";
        private static string _pwd = "";
        private static string _database = "";

    }
}
